# -*- coding: utf-8 -*-
"""
------------------------------------------------------------------------------
    File Name:  RedisDemo
    Author   :  wanwei1029
    Date     :  2018/9/29
    Desc     :
------------------------------------------------------------------------------
"""
import redis
import samp.samp_logging as sl

logger = sl.get_logger('RedisDemo')

REDIS_IP = "192.168.99.100"
REDIS_PORT = 6379
REDIS_DB = 0


def samp():
    """
    """
    r = redis.Redis(host=REDIS_IP, port=REDIS_PORT, db=REDIS_DB)
    # r.set('name', 'zhangsan')
    logger.info(r.get('name'))


def redis_pool():
    pool = redis.ConnectionPool(host=REDIS_IP, port=REDIS_PORT)
    r = redis.Redis(connection_pool=pool)
    logger.info(r.get('name'))


def redis_pipeline():
    """
    redis-py默认在执行每次请求都会创建（连接池申请连接）和断开（归还连接池）一次连接操作，如果想要在一次请求中指定多个命令，
    则可以使用pipline实现一次请求指定多个命令，并且默认情况下一次pipline 是原子性操作
    :return:
    """
    pool = redis.ConnectionPool(host=REDIS_IP, port=REDIS_PORT)
    r = redis.Redis(connection_pool=pool)
    pipe = r.pipeline(transaction=True)
    r.set('name', 'zhangsan')
    r.set('name', 'lisi')
    pipe.execute()


if __name__ == '__main__':
    test_method = "samp"
    if test_method == "samp":
        samp()
    elif test_method == "redis_pool":
        redis_pool()
    elif test_method == "redis_pipeline":
        redis_pipeline()
